package com.fengye.security;

import com.fengye.security.security.service.RoleHierarchyService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.security.crypto.password.MessageDigestPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author fengyexjtu@126.com
 * @date 2022年04月11日 7:33 PM
 */
@SpringBootApplication
@Slf4j
public class RbacApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(RbacApplication.class, args);
    }

    /*
     * 密码编码器设置
     * @author fengyexjtu@126.com
     * @date 2022/4/13 10:07 AM
     * @return org.springframework.security.crypto.password.PasswordEncoder
     */
    @Bean
    PasswordEncoder passwordEncoder() {
        val idForDefault = "bcrypt";
        val encoder = Map.of(
                idForDefault, new BCryptPasswordEncoder(),
                "SHA-1", new MessageDigestPasswordEncoder("SHA-1")
        );
        return new DelegatingPasswordEncoder(idForDefault, encoder);
    }

    @Resource
    private RoleHierarchyService roleHierarchyService;
    @Override
    public void run(String... args) throws Exception {
        String roleHierarchyExpr = roleHierarchyService.getRoleHierarchyExpr();
        log.info("角色继承关系: {}", roleHierarchyExpr);

    }
}
